﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;

namespace WPFCouse
{
    /// <summary>
    /// ScrollViewerWindow2.xaml 的交互逻辑
    /// </summary>
    public partial class ScrollViewerWindow2 : Window
    {
        public ScrollViewerWindow2()
        {
            InitializeComponent();
        }

        int index = 0;
        double ow = 0;
        /// <summary>
        /// 滚动条到了底部 true 否则 false
        /// </summary>
        bool scrollBottom = false;
        private void Button_Click(object sender, RoutedEventArgs e)
        {
            if (index >= 1) 
            {
                do
                {
                    --index;
                    if (this.sp.Children[index].GetType() == typeof(Button))
                    {
                        Button textBlock = this.sp.Children[index] as Button;
                        ow -= textBlock.ActualWidth;
                        if (textBlock.Visibility != Visibility.Collapsed)
                        {
                            break;
                        }
                    }
                    else if (this.sp.Children[index].GetType() == typeof(TextBlock))
                    {
                        TextBlock textBlock = this.sp.Children[index] as TextBlock;
                        ow -= textBlock.ActualWidth;
                        if (textBlock.Visibility != Visibility.Collapsed)
                        {
                            break;
                        }
                    }
                } while (true);
                this.ScrollViewer1.ScrollToHorizontalOffset(ow);
            }
                
            //if (index >= 1)
            //{
            //    Button textBlock;
            //    do
            //    {
            //        textBlock = this.sp.Children[--index] as Button;
            //    } while (textBlock.Visibility == Visibility.Collapsed);
            //    ow -= textBlock.ActualWidth;
            //    this.ScrollViewer1.ScrollToHorizontalOffset(ow);
            //}

            //this.ScrollViewer1.LineLeft();  // 翻到底部

            //this.ScrollViewer1.ScrollToHorizontalOffset(-20);
        }

        private void Button_Click_1(object sender, RoutedEventArgs e)
        {
            if (!scrollBottom) 
            {
                do
                {
                    index++;
                    if (this.sp.Children[index].GetType() == typeof(Button))
                    {
                        Button textBlock = this.sp.Children[index] as Button;
                        ow += textBlock.ActualWidth;
                        if (textBlock.Visibility != Visibility.Collapsed)
                        {
                            break;
                        }
                    }
                    else if (this.sp.Children[index].GetType() == typeof(TextBlock))
                    {
                        TextBlock textBlock = this.sp.Children[index] as TextBlock;
                        ow += textBlock.ActualWidth;
                        if (textBlock.Visibility != Visibility.Collapsed)
                        {
                            break;
                        }
                    }
                } while (true);
                this.ScrollViewer1.ScrollToHorizontalOffset(ow);
            }
            //if (index <= sp.Children.Count - 2) 
            //{
            //Button btn;
            //do
            //{
            //btn = this.sp.Children[index++] as Button;
            //} while (btn.Visibility == Visibility.Collapsed);
            //ow += btn.ActualWidth;
            //this.ScrollViewer1.ScrollToHorizontalOffset(ow);
            //}


            //this.ScrollViewer1.LineRight();  // 翻到底部

            //this.ScrollViewer1.ScrollToHorizontalOffset(20);

        }

        private void Button_Click_2(object sender, RoutedEventArgs e)
        {
            //btn1.Visibility = Visibility.Collapsed;
            //btn2.Visibility = Visibility.Collapsed;
            //btn3.Visibility = Visibility.Collapsed;
            //btn4.Visibility = Visibility.Collapsed;
            //btn5.Visibility = Visibility.Collapsed;
            //btn6.Visibility = Visibility.Collapsed;
            btn7.Visibility = Visibility.Collapsed;
            btn8.Visibility = Visibility.Collapsed;
            btn9.Visibility = Visibility.Collapsed;
            btn10.Visibility = Visibility.Collapsed;
            btn11.Visibility = Visibility.Collapsed;
            //btn12.Visibility = Visibility.Collapsed;
            //btn13.Visibility = Visibility.Collapsed;
            //btn14.Visibility = Visibility.Collapsed;
            //btn15.Visibility = Visibility.Collapsed;
            btn16.Visibility = Visibility.Collapsed;
            btn17.Visibility = Visibility.Collapsed;
            btn18.Visibility = Visibility.Collapsed;
            btn19.Visibility = Visibility.Collapsed;
            btn20.Visibility = Visibility.Collapsed;
            btn21.Visibility = Visibility.Collapsed;
            btn22.Visibility = Visibility.Collapsed;
            btn23.Visibility = Visibility.Collapsed;
            totalWidth = 0;
            foreach (var item in this.sp.Children)
            {
                if (item.GetType() == typeof(Button))
                {
                    Button button = item as Button;
                    if (button.Visibility == Visibility.Visible)
                        totalWidth += button.ActualWidth;
                }
                else if (item.GetType() == typeof(TextBlock))
                {
                    TextBlock textBlock = item as TextBlock;
                    if (textBlock.Visibility == Visibility.Visible)
                        totalWidth += textBlock.ActualWidth;
                }
            }
            if (totalWidth < ScrollViewer1.ActualWidth)
            {
                btnShang.Visibility = Visibility.Collapsed;
                btnXia.Visibility = Visibility.Collapsed;
            }

        }

        private void Button_Click_3(object sender, RoutedEventArgs e)
        {
            btn1.Visibility = Visibility.Visible;
            btn2.Visibility = Visibility.Visible;
            btn3.Visibility = Visibility.Visible;
            btn4.Visibility = Visibility.Visible;
            btn5.Visibility = Visibility.Visible;
            btn6.Visibility = Visibility.Visible;
            btn7.Visibility = Visibility.Visible;
            btn8.Visibility = Visibility.Visible;
            btn9.Visibility = Visibility.Visible;
            btn10.Visibility = Visibility.Collapsed;
            btn11.Visibility = Visibility.Collapsed;
            //btn12.Visibility = Visibility.Visible;
            //btn13.Visibility = Visibility.Visible;
            //btn14.Visibility = Visibility.Visible;
            //btn15.Visibility = Visibility.Visible;
            //btn16.Visibility = Visibility.Visible;
            btn17.Visibility = Visibility.Collapsed;
            btn18.Visibility = Visibility.Visible;
            btn19.Visibility = Visibility.Visible;
            btn20.Visibility = Visibility.Visible;
            btn21.Visibility = Visibility.Visible;
            btn22.Visibility = Visibility.Visible;
            btn23.Visibility = Visibility.Visible;
            totalWidth = 0;
            foreach (var item in this.sp.Children)
            {
                if (item.GetType() == typeof(Button))
                {
                    Button button = item as Button;
                    if (button.Visibility == Visibility.Visible)
                        totalWidth += button.Width;
                }
                else if (item.GetType() == typeof(TextBlock))
                {
                    TextBlock textBlock = item as TextBlock;
                    if (textBlock.Visibility == Visibility.Visible)
                        totalWidth += textBlock.Width;
                }
            }
            if (totalWidth > ScrollViewer1.ActualWidth)
            {
                btnShang.Visibility = Visibility.Visible;
                btnXia.Visibility = Visibility.Visible;
            }
        }

        double totalWidth = 0;
        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            totalWidth = 0;
            foreach (var item in this.sp.Children)
            {
                if (item.GetType() == typeof(Button))
                {
                    Button button = item as Button;
                    if (button.Visibility == Visibility.Visible)
                        totalWidth += button.Width;
                }
                else if (item.GetType() == typeof(TextBlock))
                {
                    TextBlock textBlock = item as TextBlock;
                    if (textBlock.Visibility == Visibility.Visible)
                        totalWidth += textBlock.Width;
                }
            }
        }

        private void ScrollViewer1_ScrollChanged(object sender, ScrollChangedEventArgs e)
        {
            if (e.HorizontalOffset + e.ViewportWidth == e.ExtentWidth)
            {
                scrollBottom = true;
            }
            else 
            {
                scrollBottom = false;
            }
        }
    }
}
